//GoodsServiceImpl.java
package org.example.examtemplate01.Service.impl;

import org.example.examtemplate01.Mapper.GoodsMapper;
import org.example.examtemplate01.Service.GoodsService;
import org.example.examtemplate01.module.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    //获取goods列表
    @Override
    public List<Goods> fetchGoodsList() {
        return goodsMapper.fetchGoodsList();
    }

    //删除对应ID的goods
    @Override
    public int removeGoodsById(int id) {
        return goodsMapper.removeGoodsById(id);
    }

    //新增Goods
    @Override
    public int add(Goods goods) {
        return goodsMapper.addGoods(goods);
    }

    //修改对应ID的goods
    @Override
    public Goods fetchGoodsById(int id) {
        return goodsMapper.fetchGoodsById(id);
    }

    //修改goods
    @Override
    public int edit(Goods goods) {
        return goodsMapper.editGoods(goods);
    }

    //分页获取goods列表
    @Override
    public List<Goods> fetchByPage(int currentPage, int pageSize) {
        int fromIndex  = (currentPage < 1) ? 0 : (currentPage - 1) * pageSize;
        //从 fromIndex 开始（起始位置），返回 pageSize 条数据。
        return goodsMapper.fetchByPage(fromIndex ,pageSize);
    }
}
